package cn.itcast.bos.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.fore.web.action.common.BaseAction;

@Controller
@Scope("protoptype")
public class WayBillAction extends BaseAction<WayBill> {
	@Autowired
	private WayBillService wayBillService;

	// 快速录入工作单
	@Action("wayBill_quickadd")
	public String quickadd() {
		Map<String, Object> resultMap = new HashMap<>();
		// 调用service层处理数据
		try {
			wayBillService.saveWayBill(model);
			// 封装到map中
			resultMap.put("result", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultMap.put("result", false);
		}
		// 把封装的数据压入栈顶
		ActionContext.getContext().getValueStack().push(resultMap);
		// 返回结果集
		return JSON;
	}

	// 属性注入
	private String fieldValue;
	private String fieldName;

	public void setFieldValue(String fieldValue) {
		this.fieldValue = fieldValue;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	// 分页条件查询
	@Action("wayBill_pageList")
	public String pageList() {
		// 封装条件对象,查询数据一降序查最后插入的数据在最上面，根据 id 倒序排序
		Pageable pageable = new PageRequest(page - 1, rows, new Sort(Direction.DESC, "id"));
		// 响应分页对象
		Page<WayBill> pageResponse = null;
		// 判断受否为null
		if (StringUtils.isBlank(fieldValue)) {
			// ++++++没有查询条件：直接查询数据库
			// 查询所有的分页
			pageResponse = wayBillService.findWayBillPageList(pageable);
		} else {
			// ++++++有查询条件：走es检索查询
			// 查询所有的分页
			pageResponse = wayBillService.findWayBillPageList(pageable, fieldName, fieldValue);
		}

		// 把封装的数据压入栈顶
		// ActionContext.getContext().getValueStack().push(pageResponse);
		pushPageDataToValuestackRoot(pageResponse);
		// 返回结果集
		return JSON;

	}

}
